package main

import (
	"convertImgTool/utils"
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"
	"sync/atomic"
)

type ConvertFunc func(string, string) error

var methods map[string]ConvertFunc
var count int32

var (
	inputPath      string
	outputPath     string
	format         string
	extensions     = []string{".jpg", ".jpeg", ".png", ".bmp"}
	fileFormatList []FileInfo
)

type FileInfo struct {
	ext      string
	filepath string
	fileName string
}

func main() {
	fmt.Println("初始化转换规则...")
	methods = make(map[string]ConvertFunc)
	methods[".png.jpeg"] = utils.PngToJpeg
	methods[".webp.jpeg"] = utils.WebpToJpeg
	methods[".bmp.jpeg"] = utils.BmpToJpeg
	methods[".png.jpg"] = utils.PngToJpeg
	methods[".webp.jpg"] = utils.WebpToJpeg
	methods[".bmp.jpg"] = utils.BmpToJpeg
	methods[".jpeg.png"] = utils.JpegToPng
	methods[".jpg.png"] = utils.JpegToPng
	methods[".bmp.png"] = utils.BmpToPng
	methods[".webp.png"] = utils.WebpToPng
	methods[".jpeg.bmp"] = utils.JpegToBmp
	methods[".jpg.bmp"] = utils.JpegToBmp
	methods[".png.bmp"] = utils.PngToBmp
	methods[".webp.bmp"] = utils.WebpToBmp

	flag.StringVar(&inputPath, "i", "", "图片所在目录")
	flag.StringVar(&outputPath, "o", "", "生成图片目录")
	flag.StringVar(&format, "f", "", "图片转换格式：jpg/jpeg/png/bmp")
	flag.Parse()

	fmt.Println("+-------------------+")
	fmt.Println("| 批量图片转换工具  |")
	fmt.Println("| Writen By Heckman |")
	fmt.Println("+-------------------+")
	fmt.Println("")
	if inputPath == "" || outputPath == "" || format == "" {
		fmt.Println("请输入正确的参数")
		fmt.Println("-i 图片所在目录")
		fmt.Println("-o 生成图片目录")
		fmt.Println("-f 图片转换格式：jpg/jpeg/png/bmp")
		fmt.Println("示例 ./convertImgTool -i D:/abc -o D:/abc -f jpg")
		fmt.Println("")
		return
	}

	if format != "jpg" && format != "jpeg" && format != "png" && format != "bmp" {
		fmt.Printf("不支持转换至此格式: %s \n", format)
		return
	}

	_, err := os.Open(inputPath)
	if err != nil {
		fmt.Println("输入文件目录不存在")
		return
	}

	_, err = os.Open(outputPath)
	if err != nil {
		fmt.Println("输出文件目录不存在")
		return
	}

	fmt.Println("输入参数：")
	fmt.Println("--------------------")
	fmt.Printf("输入目录：%s\n", inputPath)
	fmt.Printf("输出目录：%s\n", outputPath)
	fmt.Printf("转换格式：%s\n", format)
	fmt.Println("--------------------")
	fmt.Println("")

	files, err := os.ReadDir(inputPath)
	if err != nil {
		log.Panic(err)
		return
	}

	for _, file := range files {
		if !file.IsDir() {
			ext := strings.ToLower(filepath.Ext(file.Name()))
			for _, allowedExt := range extensions {
				if ext == strings.ToLower(allowedExt) {
					fileFormatList = append(fileFormatList, FileInfo{ext: ext, filepath: inputPath + "/" + file.Name(), fileName: strings.ReplaceAll(file.Name(), ext, "")})
				}
			}
		}
	}

	fmt.Printf("检测到 %d 个图片文件，将转换成 %s 格式 \n", len(fileFormatList), format)
	if len(fileFormatList) == 0 {
		return
	}
	fmt.Println("--------------------")

	for index, fileInfo := range fileFormatList {
		fmt.Printf(">>> 正在转换第%d个文件[%s] \n", index+1, fileInfo.fileName)
		key := fmt.Sprintf("%s.%s", fileInfo.ext, format)
		if convertFunc, exists := methods[key]; exists {
			err := convertFunc(fileInfo.filepath, filepath.Join(outputPath, fileInfo.fileName+"."+format))
			if err != nil {
				fmt.Printf("err:%v \n", err)
				continue
			}
			atomic.AddInt32(&count, 1)
		} else {
			fmt.Printf("<<< 不支持的格式: %s -> .%s\n", fileInfo.ext, format)
		}
	}
	fmt.Println("--------------------")
	fmt.Printf("转换完成, 共转换 %d 张图片\n", count)
}
